package com.wtz.temptest;

import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Date: 2020/11/2
 * Company: www.dtstack.com
 *
 * @author tiezhu
 */
public class TempDemoThree {
    private static final Pattern TIMESTAMP_PATTERN = Pattern.compile("^\\d+$");
    private static final Pattern TIMESTAMP_FORMAT_PATTERN = Pattern.compile("(\\d+-\\d+-\\d+\\s)(\\d+:\\d+:\\d+)");
    private static final Pattern TIME_FORMAT_PATTERN = Pattern.compile("\\w+\\d+:\\d+:\\d+");
    private static final Pattern DATE_FORMAT_PATTERN = Pattern.compile("\\w+\\d+-\\d+-\\d+");

    public static void main(String[] args) {
        String str = "19:05:45";
        System.out.println(convertToTimestamp(str, "time"));
        str = "2020-11-02";
        System.out.println(convertToTimestamp(str, "date"));
        str = "2020-11-02 19:05:45.742";
        System.out.println(convertToTimestamp(str, "timestamp"));
        str = "1604315145742";
        System.out.println(convertToTimestamp(str, "timestamp"));
        System.out.println(convertToTimestamp(str, "time"));
        System.out.println(convertToTimestamp(str, "date"));
        str = "2020-07-30 10:08:22.123";
        System.out.println(convertToTimestamp(str, "timestamp"));
        System.out.println(convertToTimestamp(str, "time"));
        System.out.println(convertToTimestamp(str, "date"));
    }

    /**
     * 将 2020-09-07 14:49:10.0 和 1598446699685 两种格式都转化为 Timestamp
     */
    private static Object convertToTimestamp(String timestamp, String type) {
        if (TIMESTAMP_PATTERN.matcher(timestamp).find()) {
            switch (type.toLowerCase()) {
                case ("timestamp"):
                    return new Timestamp(Long.parseLong(timestamp));
                case ("date"):
                    return new Date(new Timestamp(Long.parseLong(timestamp)).getTime());
                case ("time"):
                    return new Time(new Timestamp(Long.parseLong(timestamp)).getTime());
                default:
                    throw new RuntimeException("");
            }
        }
        Matcher matcher = TIMESTAMP_FORMAT_PATTERN.matcher(timestamp);
        if (matcher.find()) {
            switch (type.toLowerCase()) {
                case "time":
                    return Time.valueOf(String.valueOf(matcher.group(2)).trim());
                case "date":
                    return Date.valueOf(String.valueOf(matcher.group(1)).trim());
                case "timestamp":
                    return Timestamp.valueOf(timestamp);
                default:
            }
        }

        if (TIME_FORMAT_PATTERN.matcher(timestamp).find() && "time".equalsIgnoreCase(type)) {
            return Time.valueOf(timestamp);
        }
        if (DATE_FORMAT_PATTERN.matcher(timestamp).find() && "date".equalsIgnoreCase(type)) {
            return Date.valueOf(timestamp);
        }

        throw new IllegalArgumentException("Incorrect time format of timestamp");
    }
}
